package com.archie.xunji.guanzhu;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.archie.xunji.R;
import com.archie.xunji.guanzhu.entitys.Attention;
import com.lidroid.xutils.BitmapUtils;
import com.meg7.widget.CircleImageView;

import java.io.IOException;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TopicDetailActivity extends Activity implements View.OnClickListener {

    private TextView topic_detail_username;
    private TextView topic_detail_playcount;
    private TextView topic_detail_description;
    private TextView topic_detail_attitude;
    private TextView topic_detail_commentcount;
    private TextView topic_detail_createtime;
    private TextView topic_detail_location;
    private TextView topic_play_time;

    private ImageView topic_detail_headurl;
    private CircleImageView topic_detail_userimg;

    private Button topic_detail_labels;
    private ImageButton topic_detail_back;
    private ImageButton btn_list_detail_attitude;
    private ImageButton topic_detail_favourite;
    private ImageButton topic_play_music;

    private BitmapUtils bitmapUtils;
    private Attention entity;

    private boolean isflag= true;
    private MediaPlayer mPlay;
    private Thread tPlay;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_topic_detail);

        Intent intent = getIntent();
        Serializable attention = intent.getSerializableExtra("attention");
        tPlay = new Thread(new Runnable() {
            @Override
            public void run() {
                int time = (mPlay.getDuration()-mPlay.getCurrentPosition())/1000;
                while(time>0&&mPlay!=null&&isflag) {
                    try {
                        Thread.sleep(200);
                        time = (mPlay.getDuration()-mPlay.getCurrentPosition())/1000;
                        final int finalTime = time;
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                topic_play_time.setText(finalTime /60+"'"+ finalTime %60);
                            }
                        });
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        bitmapUtils = new BitmapUtils(this);
        initView();
        initData(attention);
    }

    /**
     * 数据展示
     * @param attention
     */
    private void initData(Serializable attention) {
        entity = (Attention) attention;
        topic_detail_username.setText(entity.getUsername());
        topic_detail_playcount.setText("播放 "+ entity.getPlaycount()+"");
        topic_detail_description.setText(entity.getDescription());
        topic_detail_attitude.setText(entity.getAttitudecount()+"");
        topic_detail_commentcount.setText(entity.getCommentcount()+"");
        topic_detail_location.setText(entity.getPlacename()+"");
        long createdate = entity.getCreatedate();
        SimpleDateFormat data = new SimpleDateFormat("MM-dd HH:mm");
        String format = data.format(new Date(createdate));
        topic_detail_createtime.setText(format);

        bitmapUtils.display(topic_detail_headurl, "http://imagecdn.xunjimap.com/image/" + entity.getImageurl());
        bitmapUtils.display(topic_detail_userimg, "http://imagecdn.xunjimap.com/image/" + entity.getHeadurl());
        int nowTime = Integer.parseInt(entity.getAudiolength());
        topic_play_time.setText(nowTime/60 + "'" + nowTime%60);
        topic_detail_labels.setText(entity.getLabels());



    }


    /**
     * 初始化控件
     */
    private void initView() {
        topic_detail_username = (TextView) findViewById(R.id.topic_detail_username);
        topic_detail_playcount = (TextView) findViewById(R.id.topic_detail_playcount);
        topic_detail_description = (TextView) findViewById(R.id.topic_detail_description);
        topic_detail_attitude = (TextView) findViewById(R.id.topic_detail_attitude);
        topic_detail_commentcount = (TextView) findViewById(R.id.topic_detail_commentcount);
        topic_detail_createtime = (TextView) findViewById(R.id.topic_detail_createtime);
        topic_detail_location = (TextView) findViewById(R.id.topic_detail_location);
        topic_play_time = (TextView) findViewById(R.id.topic_play_time);
        topic_detail_headurl = (ImageView) findViewById(R.id.topic_detail_headurl);
        topic_detail_userimg = (CircleImageView) findViewById(R.id.topic_detail_userimg);

        topic_detail_labels = (Button) findViewById(R.id.topic_detail_labels);
        topic_detail_back = (ImageButton) findViewById(R.id.topic_detail_back);
        btn_list_detail_attitude = (ImageButton) findViewById(R.id.btn_list_detail_attitude);
        topic_detail_favourite = (ImageButton) findViewById(R.id.topic_detail_favourite);
        topic_play_music = (ImageButton) findViewById(R.id.topic_play_music);

        topic_detail_back.setOnClickListener(this);
        btn_list_detail_attitude.setOnClickListener(this);
        topic_detail_favourite.setOnClickListener(this);
        topic_play_music.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.topic_detail_back:
                finish();
                break;
            case R.id.btn_list_detail_attitude:
                btn_list_detail_attitude.setImageResource(R.mipmap.ui_comm_attituded);
                topic_detail_attitude.setText(""+(entity.getAttitudecount()+1));
                break;
            case R.id.topic_detail_favourite:
                topic_detail_favourite.setImageResource(R.mipmap.ui_comm_favourited);
                break;
            case R.id.topic_play_music:
                if(mPlay==null){
                    mPlay = new MediaPlayer();
                    mPlay.reset();
                    try {
                        mPlay.setDataSource("http://imagecdn.xunjimap.com/image/" + entity.getAudiourl());
                        mPlay.prepare();
                        topic_play_music.setImageResource(R.mipmap.ui_detail_pause);
                        mPlay.start();
                        tPlay.start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }else{
                    if (mPlay.isPlaying()){
                        mPlay.pause();
                        topic_play_music.setImageResource(R.mipmap.ui_detail_play);
                    }else{
                        mPlay.start();
                        topic_play_music.setImageResource(R.mipmap.ui_detail_pause);
                    }
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isflag=false;
        try {
            tPlay.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (mPlay!=null){
            mPlay.stop();

            mPlay.release();
        }
    }


}
